/**
 * Created by XianTao on 2016/11/8
 * <p/>
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package org.lego.handle.exception;

import org.lego.handle.exception.configuration.ConstantMessageKey;
import org.lego.handle.exception.entity.ExceptionMessage;
import org.lego.handle.exception.service.ExceptionMessageService;
import org.lego.jpa.base.JpaBaseApplication;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.util.ObjectUtils;

@SpringBootApplication
@Import(JpaBaseApplication.class)
public class HandleExceptionApplication {
    public static void main(String[] args) {
        SpringApplication.run(HandleExceptionApplication.class, args);
    }

    @Bean(name = "initHandleExceptionApplication")
    public InitializingBean initializingBean() {
        return new InitializingBean() {
            @Autowired
            private ExceptionMessageService exceptionMessageService;

            public void afterPropertiesSet() throws Exception {
                // 插入初始化数据
                if (ObjectUtils.isEmpty(exceptionMessageService.findOne(ConstantMessageKey.EXCEPTION_MESSAGE_NEED_PARAM_ID))) {
                    exceptionMessageService.saveAndFlush(new ExceptionMessage(ConstantMessageKey.EXCEPTION_MESSAGE_NEED_PARAM_ID, "需要id字段"));
                }
                if (ObjectUtils.isEmpty(exceptionMessageService.findOne(ConstantMessageKey.EXCEPTION_MESSAGE_NOT_FOUND_BY_ID))) {
                    exceptionMessageService.saveAndFlush(new ExceptionMessage(ConstantMessageKey.EXCEPTION_MESSAGE_NOT_FOUND_BY_ID, "通过ExceptionMessage.id【%s】未找到记录"));
                }
            }
        };
    }
}
